int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int dp = 9;
//设置阳极接口
int d4 = 13;
int d3 = 12;
int d2 = 11;
int d1 = 10;
//设置按钮接口
int y_button=A0;
int b_button=A1;
int g_button=A2;

unsigned long times=0;
int m=0;
int h=0;

int i=0,j=0,x=0,y=0;
int flag=0;
bool flag2=true;
int set=0;
void setup()
{
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);

  pinMode(y_button,INPUT_PULLUP);//设置为上拉输入模式
  pinMode(b_button,INPUT_PULLUP);
  pinMode(g_button,INPUT_PULLUP);

  Serial.begin(9600);
}

void loop()
{ 
   if(digitalRead(y_button)==LOW){
    Serial.println("y");
    set++;
    while(digitalRead(y_button)==LOW);//消抖
   }
   if(digitalRead(b_button)==LOW){
    set--;
    while(digitalRead(b_button)==LOW);
   }
   if(digitalRead(g_button)==LOW){
    flag++;
    while(digitalRead(g_button)==LOW);
   }
  if(flag==0){
    h=set;
     m=0;
    flag2=true;
  }else if(flag==1){
    m=set;
   
  }else{
    while(flag2){
      flag=0;
      set=0;
      clocks();
      if(digitalRead(g_button)==LOW){
        flag2=false;
      while(digitalRead(g_button)==LOW);
     }
    }
  }
  Xian_Shi();  
    

  


}

void clocks(){//时钟逻辑
    Xian_Shi();
    if(millis()-times>59000){
      m++;
      times=millis();
      Serial.print(x);
      Serial.print(y);
      Serial.print(" ");
      Serial.print(i);
      Serial.print(j);
      Serial.println();
    }
    if(m>59){
      h++;
      m=0;
    }
    if(h>23){
      h=0;
      
    }
}

void Xian_Shi(){//数码管显示
   if(m>9){
      i=m/10;
      j=m%10;
    }else{
      j=m;
      i=0;
    }
    if(h>9){
      x=h/10;
      y=h%10;
    }else{
      y=h;
      x=0;
    }
    Display(1, x);
    Display(2, y);
    Display(3, i);
    Display(4, j);
}

void WeiXuan(unsigned char n)//数码管位数选择
{
    switch(n)
     {
        case 1:
          digitalWrite(d1,LOW);
          digitalWrite(d2, HIGH);
          digitalWrite(d3, HIGH);
          digitalWrite(d4, HIGH);   
        break;
        case 2:
          digitalWrite(d1, HIGH);
          digitalWrite(d2, LOW);
          digitalWrite(d3, HIGH);
          digitalWrite(d4, HIGH);
            break;
          case 3:
            digitalWrite(d1,HIGH);
           digitalWrite(d2, HIGH);
           digitalWrite(d3, LOW);
           digitalWrite(d4, HIGH);
            break;
          case 4:
           digitalWrite(d1, HIGH);
           digitalWrite(d2, HIGH);
           digitalWrite(d3, HIGH);
           digitalWrite(d4, LOW);
            break;
        default :
           digitalWrite(d1, HIGH);
           digitalWrite(d2, HIGH);
           digitalWrite(d3, HIGH);
           digitalWrite(d4, HIGH);
        break;
          }
}

/*
void WeiXuan(unsigned char n)//数码管位数选择
{
    switch(n)
     {
        case 1:
          digitalWrite(d1,HIGH);
          digitalWrite(d2, LOW);
          digitalWrite(d3, LOW);
          digitalWrite(d4, LOW);   
        break;
        case 2:
          digitalWrite(d1, LOW);
          digitalWrite(d2, HIGH);
          digitalWrite(d3, LOW);
          digitalWrite(d4, LOW);
            break;
          case 3:
            digitalWrite(d1,LOW);
           digitalWrite(d2, LOW);
           digitalWrite(d3, HIGH);
           digitalWrite(d4, LOW);
            break;
          case 4:
           digitalWrite(d1, LOW);
           digitalWrite(d2, LOW);
           digitalWrite(d3, LOW);
           digitalWrite(d4, HIGH);
            break;
        default :
           digitalWrite(d1, LOW);
           digitalWrite(d2, LOW);
           digitalWrite(d3, LOW);
           digitalWrite(d4, LOW);
        break;
          }
}
*/
void Num_0()//数字0
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_1()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_2()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_3()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_4()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_5()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_6()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_7()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_8()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_9()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Clear()  //清屏
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}

/*
void Num_0()//数字0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,HIGH);
}
void Num_1()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,HIGH);
}
void Num_2()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_3()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_4()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_5()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_6()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_7()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,HIGH);
}
void Num_8()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Num_9()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,HIGH);
}
void Clear()  //清屏
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,HIGH);
}

*/
void pickNumber(unsigned char n)//选择数字
{
  switch(n)
  {
   case 0:Num_0();
   break;
   case 1:Num_1();
   break;
   case 2:Num_2();
   break;
   case 3:Num_3();
   break;
   case 4:Num_4();
   break;
   case 5:Num_5();
   break;
   case 6:Num_6();
   break;
   case 7:Num_7();
   break;
   case 8:Num_8();
   break;
   case 9:Num_9();
   break;
   default:Clear();
   break;
  }
}

void Display(unsigned char x, unsigned char Number)//显示x为坐标，Number为数字
{
  WeiXuan(x);
  pickNumber(Number);
  delay(1);
  Clear();
}
